<template>
  <div class="survey-box">
    <el-row type="flex" justify="space-between" align="middle" class="survey-hedaer">
      <div class="title">课程交易分析</div>
      <box-header-right :school="hasSchool" @update:date="updateDate" @update:school="changeSchool" />
    </el-row>
    <el-row type="flex" class="user" :gutter="24">
      <el-col :span="4">
        <div class="item bg1">
          <div class="title"><count-roll :number="response.place_order_count" /></div>
          <div class="name">下单数</div>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="item bg2">
          <div class="title"><count-roll :number="response.payment_order_count" /></div>
          <div class="name">支付订单数</div>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="item bg3">
          <div class="title"><count-roll :number="response.place_user_count" /></div>
          <div class="name">下单用户数</div>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="item bg4">
          <div class="title"><count-roll :number="response.payment_user_count" /></div>
          <div class="name">支付用户数</div>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="item bg5">
          <div class="title"><count-roll :number="response.place_price" :is-money="true" /></div>
          <div class="name">下单金额</div>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="item bg6">
          <div class="title"><count-roll :number="response.payment" :is-money="true" /></div>
          <div class="name">支付金额</div>
        </div>
      </el-col>
    </el-row>
    <div class="ve-box">
      <div class="home-title">订单数与订单额</div>
      <el-row :gutter="70">
        <el-col :span="12">
          <ve-histogram
            :loading="loading"
            :data="orderCount.chartData"
            :data-empty="orderCount.chartData.rows.length == 0"
            :settings="orderCount.chartSettings"
            :colors="orderCount.colors"
            :extend="orderCount.extendsConfig"
            :legend-visible="false"
          />
        </el-col>
        <el-col :span="12">
          <ve-histogram
            :loading="loading"
            :data="payment.chartData"
            :data-empty="payment.chartData.rows.length == 0"
            :settings="payment.chartSettings"
            :colors="payment.colors"
            :extend="payment.extendsConfig"
            :legend-visible="false"
          />
        </el-col>
      </el-row>
    </div>
    <div class="trading">
      <div class="home-title">交易构成</div>
      <el-row :gutter="70">
        <el-col :span="6"><student-payment-user :rows="response.payment_rate" /></el-col>
        <el-col :span="18">
          <el-row type="flex" align="middle" class="header">
            <el-col :span="8"><div class="name">结算金额(元)</div></el-col>
            <el-col :span="8"><div class="name">用户数</div></el-col>
            <el-col :span="8"><div class="name">用户占比</div></el-col>
          </el-row>
          <el-row type="flex" justify="space-between" align="middle" class="item">
            <el-col :span="4"><div class="name">老付费用户</div></el-col>
            <el-col :span="4">
              <div class="price"><count-roll :number="response.old_user_payment" :is-money="true" /></div>
            </el-col>
            <el-col :span="8">
              <div class="number"><count-roll :number="response.old_user_count" /></div>
            </el-col>
            <el-col :span="8">
              <div>{{ response.old_user_count_rate || 0 | preSymbol }}</div>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between" align="middle" class="item">
            <el-col :span="4"><div class="name">新付费用户</div></el-col>
            <el-col :span="4">
              <div class="price"><count-roll :number="response.new_user_payment" :is-money="true" /></div>
            </el-col>
            <el-col :span="8">
              <div class="number"><count-roll :number="response.new_user_count" /></div>
            </el-col>
            <el-col :span="8">
              <div>{{ response.new_user_count_rate || 0 | preSymbol }}</div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import VeHistogram from 'v-charts/lib/histogram.common';
import 'v-charts/lib/style.css';
import BoxHeaderRight from '../../../components/daterange.vue';
import CountRoll from '@/pages/data/components/count_roll';
import StudentPaymentUser from '@/pages/vcharts/box/student/components/student_payment_user.vue';
export default {
  components: {
    VeHistogram,
    BoxHeaderRight,
    CountRoll,
    StudentPaymentUser
  },
  data() {
    const moneySymbol = this.site.money_symbol;
    return {
      daterange: null,
      mhm_id: 0,
      loading: false,
      response: {},
      orderCount: {
        dataEmpty: true,
        chartSettings: {
          labelMap: {
            order_type: '订单类型',
            order_count: '订单量'
          },
          yAxisName: ['订单量/单']
        },
        colors: ['#7BB4FF'],
        chartData: {
          columns: ['order_type', 'order_count'],
          rows: []
        },
        extendsConfig: {
          series(v) {
            if (v) {
              v.forEach(i => {
                i.barWidth = '27px';
              });
            }

            return v;
          },
          yAxis: {
            type: 'value',
            minInterval: 1
          }
        }
      },
      payment: {
        dataEmpty: true,
        chartSettings: {
          labelMap: {
            order_type: '订单类型',
            payment: '营业额'
          },
          yAxisName: ['营业额/' + this.site.money_title]
        },
        colors: ['#FF8A8A'],
        chartData: {
          columns: ['order_type', 'payment'],
          rows: []
        },
        extendsConfig: {
          series(v) {
            if (v) {
              v.forEach(i => {
                i.barWidth = '27px';
              });
              return v;
            }
          },
          tooltip(v) {
            if (v) {
              v.formatter = function(params) {
                var str = '<div>' + params[0].name.split('[')[0] + '</div>';
                for (var i = 0; i < params.length; i++) {
                  str += params[i].marker + ' ' + params[i].seriesName + '：' + moneySymbol + params[i].data + '<br />';
                }

                return str;
              };
            }
            return v;
          }
        }
      }
    };
  },
  computed: {
    hasSchool() {
      return !!this.response.is_platform;
    }
  },
  created() {
    this.loadData();
  },
  mounted() {
    this.$store.dispatch('setLoading', 0);
  },
  methods: {
    updateDate: function(date) {
      this.daterange = date;
      this.loading = true;
      this.loadData();
    },
    changeSchool: function(mhm) {
      this.mhm_id = mhm;
      this.loading = true;
      this.loadData();
    },
    loadData: function() {
      this.$http.get(
        '/admin/data/transaction/course',
        {
          daterange: this.daterange,
          mhm_id: this.mhm_id
        },
        response => {
          this.response = response.data;
          this.orderCount.chartData.rows = this.response.order_count_rows;
          this.payment.chartData.rows = this.response.payment_rows;
          this.loading = false;
        },
        errror => {
          this.loading = false;
        },
        false
      );
    }
  }
};
</script>
<style lang="scss" scoped>
.survey-box {
  background-color: #fff;
  min-height: 150px;
  margin-bottom: 20px;
  padding: 10px 20px 25px;
  .survey-hedaer {
    margin-bottom: 28px;
    height: 36px;
    padding-right: 20px;
    background-color: #f1f8ff;
    .title {
      color: #303133;
      font-size: 15px;
      padding-left: 6px;
      border-left: 2px solid #5191ff;
    }
  }
  .home-title {
    width: 423px;
    height: 30px;
    margin-bottom: 30px;
    background: linear-gradient(90deg, rgba(244, 249, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
    font-size: 14px;
    color: rgba(48, 49, 51, 1);
    line-height: 30px;
    padding: 0 10px;
  }
  .user {
    .item {
      padding: 18px 18px 24px;
      box-sizing: border-box;
      text-align: center;
      border-radius: 4px;
      &.bg1 {
        background: linear-gradient(226deg, rgba(255, 212, 65, 1) 0%, rgba(250, 165, 0, 1) 100%);
      }
      &.bg2 {
        background: linear-gradient(226deg, rgba(115, 164, 255, 1) 0%, rgba(60, 143, 255, 1) 100%);
      }
      &.bg3 {
        background: linear-gradient(226deg, rgba(255, 172, 118, 1) 0%, rgba(255, 142, 70, 1) 100%);
      }
      &.bg4 {
        background: linear-gradient(225deg, rgba(173, 232, 89, 1) 0%, rgba(131, 219, 49, 1) 100%);
      }
      &.bg5 {
        background: linear-gradient(226deg, rgba(255, 149, 132, 1) 0%, rgba(255, 113, 105, 1) 100%);
      }
      &.bg6 {
        background: linear-gradient(45deg, rgba(123, 114, 255, 1) 0%, rgba(135, 161, 255, 1) 100%);
      }
      .title {
        color: #fff;
        font-size: 26px;
        font-weight: bold;
      }
      .name {
        color: #fff;
        font-size: 14px;
        margin-top: 13px;
      }
    }
  }
  .ve-box {
    margin-top: 24px;
  }
  .trading {
    .header {
      margin-top: 30px;
      height: 44px;
      padding: 0px 40px;
      background-color: #f6f9ff;
      .name {
        font-size: 13px;
        font-weight: 400;
        color: #303133;
        text-align: right;
      }
    }
    .item {
      height: 38px;
      padding: 0px 40px;
      font-size: 12px;
      color: #606266;
      border-bottom: 1px solid #ebeef5;
      text-align: right;
      .name {
        color: #303133;
        font-size: 12px;
        text-align: left;
      }
    }
  }
}
</style>
